<?php


if ($_GET['page']== "shop" && isset ($_GET['buyitem']) )
{
	buyweapon($_GET['buyitem']);
}	

function buyweapon($id)

{

   		$q = mysql_query("SELECT * FROM item WHERE id='$id'");
   		$f = mysql_fetch_array($q);
		$name = $f['name'];
        
        $cid = $_SESSION['cid'];
		$aid = $_SESSION['uid'];
        $pollars = getPollars($aid);
        $getitem = mysql_query("SELECT * FROM item_own WHERE iid = '$id' AND cid = '$cid'");
        $itemname = $f['name'];
  

  if (mysql_num_rows($getitem) == 0 )
   {
       if (removePollar($f['price'],$aid) == 1)
  	  {
  	  	$type = $f['type'];
	    mysql_query("INSERT INTO item_own (type,iid,cid) VALUES ('$type', '$id', '$cid')");
	    logTransaction(10, $f['price'], TransactionType(10,$f['name']));
	    alert("You succesfully bought $itemname ");
	    redirect("?page=shop");
	  }
	  else
	  {
	  	alert("You don't have enough Pollars for this!");
	  	redirect("?page=shop");
	  }

     
   }
    else
   {

   	 alert("You already got this item.");
   	 redirect("?page=shop");
   } 	


    
   

   
}



function getWeaponShop()
{
	
	$cid = $_SESSION['cid'];
	$aid = $_SESSION['uid'];

	$q = mysql_query("SELECT * FROM item WHERE type = '1' ORDER BY price");
    while ($f = mysql_fetch_array($q))

    {
    		$itemid = $f['id'];
    		$getown = mysql_query("SELECT * FROM item_own WHERE iid='$itemid' AND cid='$cid'");
    		if(mysql_num_rows($getown) == 0)
    		{
    			echo'<table style="float:left; margin-right:10px;">';
			          echo'<tr>
			                <td><h4><b>'.$f['name'].'<h4></b></td>
			               </tr>
			               <tr>
			                <td><img src="img/weapons/'.$f['image'].'" height="150px" /> 
			               <tr>
			                <td>&#x2766; '.$f['price'].' </td>
			                </tr>
			                <tr>
			                <td>Attack: +'.$f['attackbonus'].'</td>
		                   </tr>
		                   <tr>
			                <td>Defence: +'.$f['defencebonus'].'</td>
		                   </tr>
		                   <tr>
			                <td>Succesrate: +'.$f['succesrate'].'</td>
		                   </tr>
		                   <tr>
		                   <td colspan="2"><a href="?page=shop&buyitem='.$f['id'].'"><button class="bsexy">Buy '.$f['name'].'</button></a></td>
		                   </tr>
			
			
			</table>';
    		}
     
   }
}

function getArmourShop()
{
	
	$cid = $_SESSION['cid'];
	$aid = $_SESSION['uid'];

	$q = mysql_query("SELECT * FROM item WHERE type = '0' ORDER BY price");
	while ($f = mysql_fetch_array($q))

    {
    		$itemid = $f['id'];
    		$getown = mysql_query("SELECT * FROM item_own WHERE iid='$itemid' AND cid='$cid'");
    		if(mysql_num_rows($getown) == 0)
    		{
    			echo'<table style="float:left; margin-right:10px;">';
			          echo'<tr>
			                <td><h4><b>'.$f['name'].'<h4></b></td>
			               </tr>
			               <tr>
			                <td><img src="img/armour/'.$f['image'].'" height="150px" /> 
			               <tr>
			                <td>&#x2766; '.$f['price'].' </td>
			                </tr>
			                <tr>
			                <td>Attack: +'.$f['attackbonus'].'</td>
		                   </tr>
		                   <tr>
			                <td>Defence: +'.$f['defencebonus'].'</td>
		                   </tr>
		                   <tr>
			                <td>Succesrate: +'.$f['succesrate'].'</td>
		                   </tr>
		                   <tr>
		                   <td colspan="2"><a href="?page=shop&buyitem='.$f['id'].'"><button class="bsexy">Buy '.$f['name'].'</button></a></td>
		                   </tr>
			
			
			</table>';
    		}
     
   }
}



?>